home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / hplip / base / g.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  11.1 KB  |  299 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import os
  6. import os.path as os
  7. import ConfigParser
  8. import locale
  9. import pwd
  10. import stat
  11. import re
  12. from codes import *
  13. import logger
  14. log = logger.Logger('', logger.Logger.LOG_LEVEL_INFO, logger.Logger.LOG_TO_CONSOLE)
  15. log.set_level('info')
  16. MINIMUM_PYQT_MAJOR_VER = 3
  17. MINIMUM_PYQT_MINOR_VER = 14
  18. MINIMUM_QT_MAJOR_VER = 3
  19. MINIMUM_QT_MINOR_VER = 0
  20.  
  21. def to_bool(s, default = False):
  22.     if isinstance(s, str) and s:
  23.         if s[0].lower() in ('1', 't', 'y'):
  24.             return True
  25.         if s[0].lower() in ('0', 'f', 'n'):
  26.             return False
  27.     elif isinstance(s, bool):
  28.         return s
  29.     s[0].lower() in ('1', 't', 'y')
  30.     return default
  31.  
  32.  
  33. class Properties(dict):
  34.     
  35.     def __getattr__(self, attr):
  36.         if attr in self.keys():
  37.             return self.__getitem__(attr)
  38.         return ''
  39.  
  40.     
  41.     def __setattr__(self, attr, val):
  42.         self.__setitem__(attr, val)
  43.  
  44.  
  45. prop = Properties()
  46.  
  47. class ConfigBase(object):
  48.     
  49.     def __init__(self, filename):
  50.         self.filename = filename
  51.         self.conf = ConfigParser.ConfigParser()
  52.         self.read()
  53.  
  54.     
  55.     def get(self, section, key, default = u''):
  56.         
  57.         try:
  58.             return self.conf.get(section, key)
  59.         except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
  60.             return default
  61.  
  62.  
  63.     
  64.     def set(self, section, key, value):
  65.         if not self.conf.has_section(section):
  66.             self.conf.add_section(section)
  67.         
  68.         self.conf.set(section, key, value)
  69.         self.write()
  70.  
  71.     
  72.     def sections(self):
  73.         return self.conf.sections()
  74.  
  75.     
  76.     def has_section(self, section):
  77.         return self.conf.has_section(section)
  78.  
  79.     
  80.     def options(self, section):
  81.         return self.conf.options(section)
  82.  
  83.     keys = options
  84.     
  85.     def read(self):
  86.         if self.filename is not None:
  87.             
  88.             try:
  89.                 fp = open(self.filename, 'r')
  90.                 self.conf.readfp(fp)
  91.                 fp.close()
  92.             except (OSError, IOError):
  93.                 log.debug('Unable to open file %s for reading.' % self.filename)
  94.             except:
  95.                 None<EXCEPTION MATCH>(OSError, IOError)
  96.             
  97.  
  98.         None<EXCEPTION MATCH>(OSError, IOError)
  99.  
  100.     
  101.     def write(self):
  102.         if self.filename is not None:
  103.             
  104.             try:
  105.                 fp = open(self.filename, 'w')
  106.                 self.conf.write(fp)
  107.                 fp.close()
  108.             except (OSError, IOError):
  109.                 log.debug('Unable to open file %s for writing.' % self.filename)
  110.             except:
  111.                 None<EXCEPTION MATCH>(OSError, IOError)
  112.             
  113.  
  114.         None<EXCEPTION MATCH>(OSError, IOError)
  115.  
  116.  
  117.  
  118. class SysConfig(ConfigBase):
  119.     
  120.     def __init__(self):
  121.         ConfigBase.__init__(self, '/etc/hp/hplip.conf')
  122.  
  123.  
  124.  
  125. class State(ConfigBase):
  126.     
  127.     def __init__(self):
  128.         ConfigBase.__init__(self, '/var/lib/hp/hplip.state')
  129.  
  130.  
  131.  
  132. class UserConfig(ConfigBase):
  133.     
  134.     def __init__(self):
  135.         if not os.geteuid() == 0:
  136.             prop.user_dir = os.path.expanduser('~/.hplip')
  137.             
  138.             try:
  139.                 if not os.path.exists(prop.user_dir):
  140.                     os.makedirs(prop.user_dir)
  141.             except OSError:
  142.                 pass
  143.  
  144.             prop.user_config_file = os.path.join(prop.user_dir, 'hplip.conf')
  145.             if not os.path.exists(prop.user_config_file):
  146.                 
  147.                 try:
  148.                     file(prop.user_config_file, 'w').close()
  149.                     s = os.stat(os.path.dirname(prop.user_config_file))
  150.                     os.chown(prop.user_config_file, s[stat.ST_UID], s[stat.ST_GID])
  151.                 except IOError:
  152.                     pass
  153.                 except:
  154.                     None<EXCEPTION MATCH>IOError
  155.                 
  156.  
  157.             None<EXCEPTION MATCH>IOError
  158.             ConfigBase.__init__(self, prop.user_config_file)
  159.         else:
  160.             prop.user_dir = None
  161.             prop.user_config_file = None
  162.             ConfigBase.__init__(self, None)
  163.  
  164.     
  165.     def workingDirectory(self):
  166.         t = self.get('last_used', 'working_dir', os.path.expanduser('~'))
  167.         
  168.         try:
  169.             t = t.decode('utf-8')
  170.         except UnicodeError:
  171.             log.error('Invalid unicode: %s' % t)
  172.  
  173.         log.debug('working directory: %s' % t)
  174.         return t
  175.  
  176.     
  177.     def setWorkingDirectory(self, t):
  178.         self.set('last_used', 'working_dir', t.encode('utf-8'))
  179.         log.debug('working directory: %s' % t.encode('utf-8'))
  180.  
  181.  
  182. os.umask(31)
  183. sys_conf = SysConfig()
  184. sys_state = State()
  185. user_conf = UserConfig()
  186.  
  187. try:
  188.     (prop.locale, prop.encoding) = locale.getdefaultlocale()
  189. except ValueError:
  190.     prop.locale = 'en_US'
  191.     prop.encoding = 'UTF8'
  192.  
  193. prop.version = sys_conf.get('hplip', 'version', '0.0.0')
  194. _p = re.compile('(\\d*)', re.I)
  195. _x = []
  196. for _y in prop.version.split('.')[:3]:
  197.     _z = _p.match(_y)
  198.     if _z is not None:
  199.         _x.append(_z.group(1))
  200.         continue
  201.  
  202. prop.installed_version = '.'.join(_x)
  203.  
  204. try:
  205.     prop.installed_version_int = []([]([ '%02x' % int(_y) for _y in _x ]), 16)
  206. except ValueError:
  207.     prop.installed_version_int = 0
  208.  
  209. prop.home_dir = sys_conf.get('dirs', 'home', os.path.realpath(os.path.normpath(os.getcwd())))
  210. prop.username = pwd.getpwuid(os.getuid())[0]
  211. pdb = pwd.getpwnam(prop.username)
  212. prop.userhome = pdb[5]
  213. prop.history_size = 50
  214. prop.data_dir = os.path.join(prop.home_dir, 'data')
  215. prop.image_dir = os.path.join(prop.home_dir, 'data', 'images')
  216. prop.xml_dir = os.path.join(prop.home_dir, 'data', 'xml')
  217. prop.models_dir = os.path.join(prop.home_dir, 'data', 'models')
  218. prop.localization_dir = os.path.join(prop.home_dir, 'data', 'localization')
  219. prop.max_message_len = 8192
  220. prop.max_message_read = 65536
  221. prop.read_timeout = 90
  222. prop.ppd_search_path = '/usr/share;/usr/local/share;/usr/lib;/usr/local/lib;/usr/libexec;/opt;/usr/lib64'
  223. prop.ppd_search_pattern = 'HP-*.ppd.*'
  224. prop.ppd_download_url = 'http://www.linuxprinting.org/ppd-o-matic.cgi'
  225. prop.ppd_file_suffix = '-hpijs.ppd'
  226. prop.gui_build = to_bool(sys_conf.get('configure', 'gui-build', '0'))
  227. prop.net_build = to_bool(sys_conf.get('configure', 'network-build', '0'))
  228. prop.par_build = to_bool(sys_conf.get('configure', 'pp-build', '0'))
  229. prop.usb_build = True
  230. prop.scan_build = to_bool(sys_conf.get('configure', 'scanner-build', '0'))
  231. prop.fax_build = to_bool(sys_conf.get('configure', 'fax-build', '0'))
  232. prop.doc_build = to_bool(sys_conf.get('configure', 'doc-build', '0'))
  233. prop.foomatic_xml_install = to_bool(sys_conf.get('configure', 'foomatic-xml-install', '0'))
  234. prop.foomatic_ppd_install = to_bool(sys_conf.get('configure', 'foomatic-ppd-install', '0'))
  235. spinner = '\\|/-\\|/-'
  236. spinpos = 0
  237.  
  238. def update_spinner():
  239.     global spinpos
  240.     if not log.is_debug() and sys.stdout.isatty():
  241.         sys.stdout.write('\x08' + spinner[spinpos])
  242.         spinpos = (spinpos + 1) % 8
  243.         sys.stdout.flush()
  244.     
  245.  
  246.  
  247. def cleanup_spinner():
  248.     if not log.is_debug() and sys.stdout.isatty():
  249.         sys.stdout.write('\x08 \x08')
  250.         sys.stdout.flush()
  251.     
  252.  
  253. ERROR_STRINGS = {
  254.     ERROR_SUCCESS: 'No error',
  255.     ERROR_UNKNOWN_ERROR: 'Unknown error',
  256.     ERROR_DEVICE_NOT_FOUND: 'Device not found',
  257.     ERROR_INVALID_DEVICE_ID: 'Unknown/invalid device-id field',
  258.     ERROR_INVALID_DEVICE_URI: 'Unknown/invalid device-uri field',
  259.     ERROR_DATA_LENGTH_EXCEEDS_MAX: 'Data length exceeds maximum',
  260.     ERROR_DEVICE_IO_ERROR: 'Device I/O error',
  261.     ERROR_NO_PROBED_DEVICES_FOUND: 'No probed devices found',
  262.     ERROR_DEVICE_BUSY: 'Device busy',
  263.     ERROR_DEVICE_STATUS_NOT_AVAILABLE: 'DeviceStatus not available',
  264.     ERROR_INVALID_SERVICE_NAME: 'Invalid service name',
  265.     ERROR_ERROR_INVALID_CHANNEL_ID: 'Invalid channel-id (service name)',
  266.     ERROR_CHANNEL_BUSY: 'Channel busy',
  267.     ERROR_DEVICE_DOES_NOT_SUPPORT_OPERATION: 'Device does not support operation',
  268.     ERROR_DEVICEOPEN_FAILED: 'Device open failed',
  269.     ERROR_INVALID_DEVNODE: 'Invalid device node',
  270.     ERROR_INVALID_HOSTNAME: 'Invalid hostname ip address',
  271.     ERROR_INVALID_PORT_NUMBER: 'Invalid JetDirect port number',
  272.     ERROR_NO_CUPS_QUEUE_FOUND_FOR_DEVICE: 'No CUPS queue found for device.',
  273.     ERROR_DATFILE_ERROR: 'DAT file error',
  274.     ERROR_INVALID_TIMEOUT: 'Invalid timeout',
  275.     ERROR_IO_TIMEOUT: 'I/O timeout',
  276.     ERROR_FAX_INCOMPATIBLE_OPTIONS: 'Incompatible fax options',
  277.     ERROR_FAX_INVALID_FAX_FILE: 'Invalid fax file',
  278.     ERROR_FAX_FILE_NOT_FOUND: 'Fax file not found',
  279.     ERROR_INTERNAL: 'Unknown internal error' }
  280.  
  281. class Error(Exception):
  282.     
  283.     def __init__(self, opt = ERROR_INTERNAL):
  284.         self.opt = opt
  285.         self.msg = ERROR_STRINGS.get(opt, ERROR_STRINGS[ERROR_INTERNAL])
  286.         log.debug('Exception: %d (%s)' % (opt, self.msg))
  287.         Exception.__init__(self, self.msg, opt)
  288.  
  289.  
  290.  
  291. try:
  292.     True
  293. except NameError:
  294.     True = 1 == 1
  295.     False = not True
  296.  
  297. supported_locales = {
  298.     'en_US': ('us', 'en', 'en_us', 'american', 'america', 'usa', 'english') }
  299.